package com.sbio.udiapp.ui;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.sbio.udiapp.util.DialogUtil;
import com.sbio.udiapp.util.IntentUtil;

import java.util.concurrent.atomic.AtomicInteger;

public abstract class BaseBroadCastActivity extends AppCompatActivity {

    AtomicInteger progressCount = new AtomicInteger();

    public BroadcastReceiver broadcastReceiver;

    public IntentFilter intentFilter;

    ProgressDialog progressDialog;

    /**
     * 初始化进度条
     */
    public void initProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("加载中..."); // 设置提示信息
        progressDialog.setCancelable(true); // 设置是否允许用户通过点击屏幕或按返回键取消对话框
//        progressDialog.show(); // 显示对话框
    }

    public void showProcess() {
        progressCount.incrementAndGet();
        progressDialog.show();
    }

    public void hideProcess() {
        int count = progressCount.decrementAndGet();
        if (count <= 0) {
            progressDialog.hide();
        }
    }

    public void initSystemScanData() {

        // 新建一个广播接收对象，实现数据接收和处理
        broadcastReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                try {
                    String action = intent.getAction();
                    if (action==null) {
                        return;
                    }
                    String data = IntentUtil.getStringExtra(intent);

                    switch (action) {
                        case IntentUtil.INTENT_ACTIONS_DEFAULT_1:
                        case IntentUtil.INTENT_ACTIONS_DEFAULT_2:
                            onBroadcastReceiver(data);
                        default:
                            break;
                    }
                } catch (Exception e) {
                    DialogUtil.showCommonAlertDialog(e.getMessage());
                }
            }
        };
        intentFilter = new IntentFilter();
        intentFilter.addAction(IntentUtil.INTENT_ACTIONS_DEFAULT_1);
        intentFilter.addAction(IntentUtil.INTENT_ACTIONS_DEFAULT_2);
    }

    abstract public void onBroadcastReceiver(String barcodeData);


    @Override
    protected void onResume() {
        super.onResume();
        // 注册广播接收器
        registerReceiver(broadcastReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        if (broadcastReceiver != null) {
            unregisterReceiver(broadcastReceiver); // 取消广播注册
        }
        super.onPause();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initSystemScanData();
        //初始化进度条
        initProgressDialog();
    }
}
